<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mis_data_perangkat extends MY_Controller {
	
	// Define Allowed Extension for File Upload
	var $arr_allowed_upload	= array('txt','jpg', 'jpeg', 'png', 'bmp', 'gif','ppt','pptx','xls','xlsx','doc','docx','pdf');
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
		$this->load->model('mis_data_perangkat/sample_master_model', 'Master');
		//$this->load->model('mis_data_perangkat/sample_reference_model', 'Ref');
		//$this->load->model('mis_data_perangkat/sample_detail2_model', 'Detail2');
		//$this->load->model('mis_data_perangkat/sample_detail_file_model', 'DetailFile');
		//$this->load->model('mis_data_perangkat/combo_model', 'Combo');
		$this->load->model('mis_combo_model', 'Combo');
		
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		// Display Index
		$data['title'] = $this->session_menu_label;
		$data['unitkerja']	= $this->Master->Departemen();	
		viewByTemplate('mis_data_perangkat/index', $data, 'grid');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Master->GetJSON();
	}
	
	
	function create()
	{
		// Display Form Create
		$data['title'] = "Tambah Data Perangkat";
		$data['data'] = NULL;
		
		// Define Combo Data
		$data['arr_reference']	= $this->Combo->SampleReference();
		$data['arr_province']	= $this->Combo->SWProvince();

		$query = $this->db->query("SELECT id_perangkat FROM (SELECT * FROM mis_barang ORDER BY id_perangkat DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_perangkat'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_perangkat_auto'] = $autonum;
		$data['id_perangkat_edit'] = get('id_perangkat');
		
		$data['mode'] = "create";
		viewByTemplate('mis_data_perangkat/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Perangkat";
		$data['data'] = $this->Master->GetSingleView(get('id_perangkat'));
		

		$query = $this->db->query("SELECT id_perangkat FROM (SELECT * FROM mis_barang ORDER BY id_perangkat DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_perangkat'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_perangkat_auto'] = $autonum;
		$data['id_perangkat_edit'] = get('id_perangkat');
		$data['id_barang_edit'] = get('id_barang');
		//var_dump($data['data']);

		$data['mode'] = "edit";
		viewByTemplate('mis_data_perangkat/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Perangkat";
		$data['data'] = $this->Master->GetSingleView(get('id_perangkat'));
		
		// Define Combo Data
		$data['arr_reference']	= $this->Combo->SampleReference();
		$data['arr_province']	= $this->Combo->SWProvince();
		
		$data['file_allowed']		= implode(', ', $this->arr_allowed_upload);
		
		$data['mode'] = "view";
		viewByTemplate('mis_data_perangkat/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");


		// Validation
		if (post("id_perangkat") == '') {
			$error->Add("id_perangkat", "Primary Key", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM sample_master WHERE UPPER(pk_master) = '".@strtoupper(@trim(post("pk_master")))."'")) > 0) {
			$error->Add("id_perangkat", "Primary Key", $tpl_duplicate);
		} else if (post("kode_barang") == '') {
			$error->Add("kode_barang", "Kode Barang", $tpl_empty);
		} else if (post("nama_barang") == '') {
			$error->Add("nama_barang", "Nama Barang", $tpl_empty);
		} else if (post("harga") == '') {
			$error->Add("harga", "Harga Barang", $tpl_empty);
		} else if (post("jumlah") == '') {
			$error->Add("jumlah", "Jumlah", $tpl_empty);
		} else if (post("kondisi_barang") == '') {
			$error->Add("kondisi_barang", "Kondisi Barang", $tpl_empty);
		} else if (post("nama_merek") == '') {
			$error->Add("nama_merek", "Primary Key", $tpl_empty);
		}
		
		return $error;
	}
	
	function ajax_post()
	{
		// Ajax Response For Submit Form
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				$notif="- No Record Updated -";	

				// Fill Post Data Array
					$data	=  array(
								'id_perangkat' => $this->input->post('id_perangkat'),
								'kode_barang' => $this->input->post('kode_barang'),
								'kondisi_barang' => $this->input->post('kondisi_barang'),
								'nama_barang' => $this->input->post('nama_barang'),
								'nama_merek' => $this->input->post('nama_merek'),
								'spesifikasi' => $this->input->post('spesifikasi'),
								'tipe' => $this->input->post('tipe'),
								'harga' => $this->input->post('harga'),
								'jumlah' => $this->input->post('jumlah'),
								'keterangan' => $this->input->post('keterangan'),
								'id_barang' => $this->input->post('id_barang'),
								//'stock_persediaan' => $notif
							);
				

				if (post("mode") == "create")
				{

					$this->Master->Add($data);
					
					$ls_response = "Data Tersimpan";
				}
				else if (post("mode") == "edit")
				{
					
					$this->Master->Update(	$data, 
											array('id_perangkat'	=> $this->input->post('id_perangkat')));
					
					$ls_response = "Data Tersimpan [Edited]";
				}
				
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}
	
	function ajax_delete()
	{
		$ls_result	= "";
		$pesan		= "Data Terhapus";
		$id_perangkat	= get('id_perangkat');
		
		if ($id_perangkat != "")
		{
			// Delete Single
			$arr_rule	= array('id_perangkat' => $id_perangkat);
			$ls_result = $this->Master->DeleteSingle($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_perangkat' => $row['id_perangkat']);
				$ls_result .= $this->Master->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
		echo $pesan;
	}
}
?>